package com.example.time_bank_demo.service;

import com.example.time_bank_demo.dao.TbUserDao;
import com.example.time_bank_demo.exception.TimeBankException;
import com.example.time_bank_demo.model.TbUser;
import com.example.time_bank_demo.vo.UserVo;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    private final TbUserDao tbUserDao;

    private final CodeCheckService codeCheckService;

    public UserServiceImpl(TbUserDao tbUserDao, CodeCheckService codeCheckService) {
        this.tbUserDao = tbUserDao;
        this.codeCheckService = codeCheckService;
    }

    @Override
    public UserVo login(String phone, String code) {

        //调用数据库结构 看看这个phone 它存不存在
        TbUser tbUser = tbUserDao.selectUserByPhone(phone);
        if (tbUser == null) {
            throw new TimeBankException(5, "用户不存在");
        }
        //TODO 验证码校验
        if (codeCheckService.checkCode(phone, code)) {
            return new UserVo(tbUser);
        }
        throw new TimeBankException(6, "验证码错误");
    }
}
